﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Base;
using BLL;
using DTO;

namespace CRM.GUI.QLHeThong
{
    public partial class ucResultSts : UserControl
    {
        public ucResultSts()
        {
            InitializeComponent();
            treeResultSts.BestFitColumns();
            sptpan.Panel2Collapsed = true;
            IntialLoad();
        }

        private int addcase = 0;
        private int expand = 0;
        private void IntialLoad()
        {
            List<ResultStatusEntity> list = SharedData.ResultStatusList;
            treeResultSts.KeyFieldName = "ResultID";
            treeResultSts.ParentFieldName = "Parent_Code";
            treeResultSts.DataSource = list;
        }
        private ResultStatusEntity _Result;
        private void treeResultSts_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            _Result = (ResultStatusEntity)treeResultSts.GetDataRecordByNode(e.Node);
        }

        private void mbtnAddChild_Click(object sender, EventArgs e)
        {
            addcase = 1;
            grbResult.Text = "Thêm kết quả con cho cuộc gọi";
            sptpan.Panel2Collapsed = false;
            txtResultCode.Focus();
        }

        private void mbtnAddRoot_Click(object sender, EventArgs e)
        {
            addcase = 2;
            sptpan.Panel2Collapsed = false;
            txtResultCode.Focus();
        }

        private void CheckInput()
        {
            if (string.IsNullOrWhiteSpace(txtResultCode.Text))
            {
                MessageBox.Show("Mã kết quả không được để trống", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                addcase = 0;
                return;
            }
            if (string.IsNullOrWhiteSpace(txtResultName.Text))
            {
                MessageBox.Show("Tên kết quả không được để trống", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                addcase = 0;
                return;
            }
        }

        private void btnAddResult_Click(object sender, EventArgs e)
        {
            CheckInput(); 
            cDataInTransfer data = new cDataInTransfer();
            ResultStatusEntity re = new ResultStatusEntity();
            string message = "";
            switch (addcase)
            { 
                case 0:
                    return;

                case 1:                                       
                    re.Result_Code = txtResultCode.Text.Trim();
                    re.ResultName = txtResultName.Text.Trim();
                    re.Description = txtResultDescription.Text.Trim();
                    re.Result_Level = _Result.Result_Level;
                    re.Parent_Code = _Result.ResultID;
                    try
                    {
                        data.AddNewResultStatus(re, "child", ref message);
                        MessageBox.Show("Thêm thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    catch
                    {
                        MessageBox.Show(message);
                    }
                    break;

                case 2:
                    re.Result_Code = txtResultCode.Text.Trim();
                    re.ResultName = txtResultName.Text.Trim();
                    re.Description = txtResultDescription.Text.Trim();
                    re.Parent_Code = 0;
                    try
                    {
                        data.AddNewResultStatus(re, "root", ref message);
                        MessageBox.Show("Thêm thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    catch
                    {
                        MessageBox.Show(message);
                    }
                    break;

                case 3:
                    re.ResultID = _Result.ResultID;
                    re.Result_Code = txtResultCode.Text.Trim();
                    re.ResultName = txtResultName.Text.Trim();
                    re.Description = txtResultDescription.Text.Trim();
                    try
                    {
                        data.UpdateResultStatus(re, ref message);
                        MessageBox.Show("Cập nhật thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    catch
                    {
                        MessageBox.Show(message);
                    }
                    break;
            }
            sptpan.Panel2Collapsed = true;
            txtResultCode.Text = null;
            txtResultName.Text = null;
            txtResultDescription.Text = null;
            treeResultSts.DataSource = data.LoadResultStatusList();
            if (expand == 1)
            {
                treeResultSts.ExpandAll();
            }
            else
            {
                treeResultSts.CollapseAll();
            }
        }

        private void treeResultSts_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                DevExpress.XtraTreeList.TreeList tree = sender as DevExpress.XtraTreeList.TreeList;
                DevExpress.XtraTreeList.TreeListHitInfo hitInfo = tree.CalcHitInfo(e.Location);
                _Result = (ResultStatusEntity)treeResultSts.GetDataRecordByNode(hitInfo.Node);
                if (_Result == null)
                {
                    mbtnAddChild.Enabled = false;
                    mbtnUpdateNode.Enabled = false;
                    mbtnDisable.Enabled = false;
                }
                else
                {
                    mbtnAddChild.Enabled = true;
                    mbtnUpdateNode.Enabled = true;
                    mbtnDisable.Enabled = true;
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            sptpan.Panel2Collapsed = true;
            txtResultCode.Text = null;
            txtResultName.Text = null;
            txtResultDescription.Text = null;
        }

        private void mbtnUpdateNode_Click(object sender, EventArgs e)
        {
            addcase = 3;
            grbResult.Text = "Cập nhật kết quả cuộc gọi";
            btnAddResult.Text = "Cập nhật";
            sptpan.Panel2Collapsed = false;
            txtResultCode.Focus();
        }

        private void mbtnCollapse_CheckStateChanged(object sender, EventArgs e)
        {
            if (mbtnCollapse.CheckState == CheckState.Checked)
            {
                treeResultSts.CollapseAll();
                expand = 0;
                mbtnExpand.CheckState = CheckState.Unchecked;
            }
        }

        private void mbtnExpand_CheckStateChanged(object sender, EventArgs e)
        {
            if (mbtnExpand.CheckState == CheckState.Checked)
            {
                treeResultSts.ExpandAll();
                expand = 1;
                mbtnCollapse.CheckState = CheckState.Unchecked;
            }
        }

        private void mbtnDisable_Click(object sender, EventArgs e)
        {
            ResultStatusEntity re = new ResultStatusEntity();
            cDataInTransfer data = new cDataInTransfer();
            re.ResultID = _Result.ResultID;
            re.Result_Code = _Result.Result_Code;
            re.Result_Level = _Result.Result_Level;
            re.ResultName = _Result.ResultName;
            re.Parent_Code = _Result.Parent_Code;
            re.Description = _Result.Description;
            re.IsDisable = true;
            string message = "";
            data.UpdateResultStatus(re, ref message);
        }

        private void treeResultSts_CellValueChanging(object sender, DevExpress.XtraTreeList.CellValueChangedEventArgs e)
        {
            // lay du lieu cua cot dang tuong tac
            _Result = (ResultStatusEntity)treeResultSts.GetDataRecordByNode(e.Node);
            int index = e.Node.Id;
            ResultStatusEntity re = new ResultStatusEntity();
            cDataInTransfer data = new cDataInTransfer();
            re.ResultID = _Result.ResultID;
            re.IsDisable = (bool)e.Value;
            try
            {
                data.DisableResultStatus(re);
            }
            catch
            {
                MessageBox.Show("Có lỗi xảy ra trong quá trình thực hiện\nXin vui lòng thực hiện lại.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            treeResultSts.DataSource = data.LoadResultStatusList();
            if (expand == 1)
            {
                treeResultSts.ExpandAll();
            }
            else
            {
                treeResultSts.CollapseAll();
            }
            treeResultSts.FocusedNode = treeResultSts.FindNodeByID(index);
        }
    }
}
